package com.gzy.net_app.ui.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.bumptech.glide.Glide;
import com.gzy.net_app.MainActivity;
import com.gzy.net_app.R;
import com.gzy.net_app.adapter.NewFaceAdapter;
import com.gzy.net_app.api.ApiService;
import com.gzy.net_app.application.AppContext;
import com.gzy.net_app.bean.ImageInfo;
import com.gzy.net_app.databinding.ActivityOneImageInfoBinding;
import com.gzy.net_app.ui.BaseActivity;
import com.gzy.network.NetworkApi;
import com.gzy.network.observer.BaseObserver;
import com.gzy.network.utils.KLog;
import com.orhanobut.hawk.Hawk;
import java.util.List;
import okhttp3.ResponseBody;
import org.json.JSONException;
import org.json.JSONObject;

public class OneImageInfoActivity extends BaseActivity<ActivityOneImageInfoBinding> {

  ImageInfo imageInfo;
  private NewFaceAdapter newFaceAdapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    newFaceAdapter = new NewFaceAdapter(this);


    int ipid = getIntent().getIntExtra("imageInfoId", 1000000);
    if (ipid == 1000000) {
      AppContext.showToast("暂无数据");
      return;
    }
    getOneImageInfo(ipid + "");

  }

  private void onListener() {
    binding.ivReturn.setOnClickListener(view -> finish());

    binding.ivImageInfoShareImage.setOnClickListener(view -> AppContext.showToast("分享，暂未开发"));
    binding.btImageInfoDelete.setOnClickListener(view -> deleteImageInfo());
  }

  private void initData() {
    // if (getIntent().getSerializableExtra("imageInfoId") == null) {
    //   AppContext.showToast("暂无数据");
    //   return;
    // }
    // imageInfo = (ImageInfo) getIntent().getSerializableExtra("imageInfo");



    Glide.with(this)
        .load(imageInfo.getImageUrl())
        .placeholder(R.drawable.ic_baseline_insert_photo_24)
        .into(binding.ivImageInfoImageView);
    binding.ivImageInfoPhotoTitle.setText("" + imageInfo.getImageText());

    // identifyFacePhoto();
    //获取底部导航图标颜色，根据图标颜色设置文字颜色
    Resources resource = getResources();
    if (imageInfo.getPublishStatus().equals("public")) {
      binding.ivImageInfoPhotoGong.setText("公开");
      binding.ivImageInfoPhotoGong.setTextColor(resource.getColorStateList(R.color.blue));
    } else if (imageInfo.getPublishStatus().equals("private")) {
      binding.ivImageInfoPhotoGong.setText("私有");
      binding.ivImageInfoPhotoGong.setTextColor(resource.getColorStateList(R.color.red));
    } else {
      binding.ivImageInfoPhotoGong.setText(imageInfo.getImageText());
      binding.ivImageInfoPhotoGong.setTextColor(resource.getColorStateList(R.color.yellew));
    }

    binding.ivImageInfoPhotoTime.setText("" + imageInfo.getPublishTime());

  }

  //获得单个作品数据
  private void getOneImageInfo(String s) {
    NetworkApi.createService(ApiService.class)
        .getOneImageInfoApi("" + s, "" + Hawk.get("token"))
        .compose(NetworkApi.applySchedulers(new BaseObserver<ImageInfo>() {
          @Override
          public void onSuccess(ImageInfo mImageInfo) {
            if (mImageInfo != null) {


              imageInfo = mImageInfo;
              KLog.d(TAG, imageInfo.toString());

              if (imageInfo.getFaceCount()>0){
                showListFace(imageInfo.getFaces());
              }
              initData();
              onListener();
            } else {
              KLog.d(TAG, "查询单个数据为空");
              startActivity(new Intent(OneImageInfoActivity.this, MainActivity.class));
              finish();
            }
          }

          @Override
          public void onFailure(Throwable e) {
            AppContext.showToast("查询失败");
            KLog.d(TAG, "查询失败");
            KLog.e(TAG, e.toString());
            startActivity(new Intent(OneImageInfoActivity.this, MainActivity.class));
            finish();
          }
        }));
  }

  //删除作品
  private void deleteImageInfo() {
    KLog.d(TAG, "" + imageInfo.getId());
    showWaitDialog("删除中");
    NetworkApi.createService(ApiService.class)
        .deleteOneImageInfoApi("" + imageInfo.getId(), "" + Hawk.get("token"))
        .compose(NetworkApi.applySchedulers(new BaseObserver<ResponseBody>() {
          @Override
          public void onSuccess(ResponseBody responseBody) {
            String result = AppContext.doJson(responseBody);
            String delete = null;
            try {
              JSONObject jsonObject = new JSONObject(result);
              delete = jsonObject.getString("data");
              KLog.d(TAG, delete);

            } catch (JSONException e) {
              e.printStackTrace();
            }
            if (delete != null) {
              AppContext.showToast("删除成功");
              hideWaitDialog();

              startActivity(new Intent(OneImageInfoActivity.this, MainActivity.class));
              finish();
              // binding.tvIdentifyFaceRusult.setText(AppContext.doJson(responseBody));
            } else {
              hideWaitDialog();
              AppContext.showToast("删除失败");
            }
          }

          @Override
          public void onFailure(Throwable e) {
            hideWaitDialog();
            AppContext.showToast("删除失败");
            KLog.d(TAG, "删除失败");
            KLog.e(TAG, e.toString());

          }
        }));
  }

  //展示泪飙
  private void showListFace(List<ImageInfo.FacesBean> faceListBean) {
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    binding.ryFacePeople.setLayoutManager(layoutManager);
    binding.ryFacePeople.setAdapter(newFaceAdapter);

    hideWaitDialog();
    newFaceAdapter.updateFaceList(faceListBean);
  }
}